/************************************  

DESCRIPTION: This file puts together the block estimates for men

Datasets used:
 (1) maleHypBlocks
 (2) malebetaBlockHyp_lt20
 (3) malebetaBlockHyp_2034
 (4) malebetaBlockHyp_gt34
 (5) maleDiabBlocks
 (6) malebetaBlockDiab
 (7) maleAllBlocks
 (8) malebetaBlockDeath
 
Datasets created:
 (1) malebetaHyp_overall 
 (2) malebetaDiab_overall 
 (3) maleblockDeath_overall 
 

************************************/  

set more off
capture log close
clear all

set matsize 10000
cd "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\Blocking"
global origData "N:\MedicareClaims-P045601-BE"
*Use data from LPM
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\LPM"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"
global output

adopath +  "N:/SIL-Common/estout"
adopath +  "N:/SIL-Common/outreg2"
adopath +  "N:/SIL-Common/reghdfe-master/package"

log using "$logs/3.Blocking_Male_combined.log", replace


****************Death****************

*Block size
tempfile blksize
use ./maleAllBlocks.dta
 egen totTreat = sum(VMERGER)
 egen blkTreat = sum(VMERGER), by(block)

 keep block totTreat blkTreat
 duplicates drop
 gen shareblk= blkTreat/totTreat
 sort block
save `blksize', replace
clear


	*Sort lower blocks
	use  ./malebetaBlockDeath.dta
		gen id=_n
		order coefnames*
		*Keep only postmerger
		keep if rndeath_1 =="1.postmerger"
		gen i=1
		reshape long rn sesq_ beta_, i(i) j(dis) str
		drop i rn
		gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
		 destring block, replace
		gen outcome = regexr(dis,"[\_][0-9]+","")

		sort block 

		merge m:1 block using `blksize'
		drop _merge

		*Treatment effect on the treated
		sort outcome block
		*Weights are treatment on the treated
		gen wbeta= beta_*shareblk
		gen wsesq= sesq_*shareblk
		*Treatment effect
		egen te = sum(wbeta), by(outcome)
		*Variance
		egen te_var = sum(wsesq), by(outcome)
		*SE
		gen te_se = sqrt(te_var) 

	save ./maleblockDeath_overall.dta, replace
	clear


****************DIABETES****************

*Block size
tempfile blksize
use ./maleDiabBlocks.dta
 egen totTreat = sum(VMERGER)
 egen blkTreat = sum(VMERGER), by(block)

 keep block totTreat blkTreat
 duplicates drop
 gen shareblk= blkTreat/totTreat
 sort block
save `blksize', replace
clear

*Combine diabetes results
*Sort upper blocks

tempfile diab1
use ./malebetaBlockDiab_lt15.dta
rename rndeath_1 coefnames
drop rn*
drop *15 *16
gen id=_n
sort id
save `diab1'
clear

use ./malebetaBlockDiab_gt15.dta
drop rn*
drop *15 *16
gen id=_n
sort id
merge 1:1 id using `diab1'
drop _merge
drop if beta_death_1==.

*Keep only postmerger
keep if coefnames =="1.postmerger"
gen i=1
reshape long rn sesq_ beta_, i(i) j(dis) str
drop i rn 
gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
 destring block, replace
gen outcome = regexr(dis,"[\_][0-9]+","")

sort block 

merge m:1 block using `blksize'
drop _merge

*Treatment effect on the treated
sort outcome block
*Weights are treatment on the treated
gen wbeta= beta_*shareblk
gen wsesq= sesq_*shareblk
*Treatment effect
egen te = sum(wbeta), by(outcome)
*Variance
egen te_var = sum(wsesq), by(outcome)
*SE
gen te_se = sqrt(te_var) 

save ./malebetaDiab_overall.dta, replace
clear

****************HYPERTENSION****************

	tempfile blksize
	use ./maleHypBlocks.dta
	 egen totTreat = sum(VMERGER)
	 egen blkTreat = sum(VMERGER), by(block)

	 keep block totTreat blkTreat
	 duplicates drop
	 gen shareblk= blkTreat/totTreat
	 sort block
	save `blksize', replace
clear


	use ./malebetaBlockHyp.dta
		rename rnacuteicd8_1 coefnames
		sort coefnames
		drop if beta_acuteicd8_26==.

		*Keep only postmerger
		keep if coefnames =="1.postmerger"
		gen i=1
		reshape long rn sesq_ beta_, i(i) j(dis) str
		drop i rn
		gen block = regexr(dis,"^[A-Za-z0-9]+[\_]","")
		 destring block, replace
		gen outcome = regexr(dis,"[\_][0-9]+","")

		sort block 

		merge m:1 block using `blksize'
		drop _merge

		*Treatment effect on the treated
		sort outcome block
		*Weights are treatment on the treated
		gen wbeta= beta_*shareblk
		gen wsesq= sesq_*shareblk
		*Treatment effect
		egen te = sum(wbeta), by(outcome)
		*Variance
		egen te_var = sum(wsesq), by(outcome)
		*SE
		gen te_se = sqrt(te_var) 

	save ./malebetaHyp_overall.dta, replace
